﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
using System.IO;
using BellAssistant.Common.CommonHelper;

namespace BellAssistant
{
    public partial class FrmMainBase : RibbonForm
    {
        //public ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        private static string RoamingPath = Path.Combine(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), Application.CompanyName), Application.ProductName); //C:\Users\****\AppData\Roaming\XZHKJ\SSCMS\
        private static string iniFilePath = Path.Combine(RoamingPath, "sscms.pdb");
        public INIFileHelper iniFile = null;

        public FrmMainBase()
        {
            InitializeComponent();
            iniFile = new INIFileHelper(iniFilePath);
        }

        /// <summary>
        /// 弹出空白图标对话框
        /// </summary>
        /// <param name="message"></param>
        public void ShowNoneMsgBox(string message)
        {
            MessageBox.Show(message, Msg.Infor, MessageBoxButtons.OK, MessageBoxIcon.None);
        }

        /// <summary>
        /// 弹出信息图标对话框
        /// </summary>
        /// <param name="message"></param>
        public void ShowInfoMsgBox(string message)
        {
            MessageBox.Show(message, Msg.Infor, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        /// <summary>
        /// 弹出警告图标对话框
        /// </summary>
        /// <param name="message"></param>
        public void ShowWarningMsgBox(string message)
        {
            MessageBox.Show(message, Msg.Warning, MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        /// <summary>
        /// 弹出错误图标对话框
        /// </summary>
        /// <param name="message"></param>
        public void ShowErrorMsgBox(string message)
        {
            MessageBox.Show(message, Msg.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        /// <summary>
        /// 弹出询问图标对话框
        /// </summary>
        /// <param name="message">消息</param>
        /// <param name="warningTitle">对话框标题默认显示'提示',否则显示'警告'</param>
        public void ShowQuestionMsgBox(string message, bool warningTitle = false)
        {
            if (warningTitle)
                MessageBox.Show(message, Msg.Warning, MessageBoxButtons.OK, MessageBoxIcon.Question);
            else
                MessageBox.Show(message, Msg.Infor, MessageBoxButtons.OK, MessageBoxIcon.Question);
        }

        /// <summary>
        /// 记录系统异常信息到日志
        /// </summary>
        /// <param name="message"></param>
        public void RecordErrorMessage(string message)
        {
            RecordLogs(message);
        }

        /// <summary>
        /// 记录日志
        /// </summary>
        /// <param name="message"></param>
        private void RecordLogs(string message)
        {
            string logFilePath = Path.Combine(Application.StartupPath, "Logs");
            string logName = DateTime.Now.ToString("yyyyMMdd") + ".log";
            string fileName = Path.Combine(logFilePath, logName);
            DirectoryInfo dic = new DirectoryInfo(logFilePath);
            if (!dic.Exists)
            {
                dic.Create();
            }

            System.IO.FileInfo file = new System.IO.FileInfo(fileName);
            if (!file.Exists)
            {
                using (FileStream fs = file.Create())
                {
                    fs.Close();
                    fs.Dispose();
                }
            }
            else
            {
                using (StreamWriter sw = new StreamWriter(fileName, true, Encoding.Default))
                {
                    StringBuilder log = new StringBuilder(Environment.NewLine);
                    log.AppendFormat("异常时间：{0}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                    if (LoginInfo.CurrentUser != null)
                    {
                        log.Append(Environment.NewLine);
                        log.AppendFormat("登录帐号：{0}，\t姓名：{1}", LoginInfo.CurrentUser.LoginName, LoginInfo.CurrentUser.UserName);
                    }
                    log.Append(Environment.NewLine);
                    log.AppendFormat("异常信息：{0}", message);

                    sw.Flush();
                    sw.Write(log);
                    sw.Close();
                }
            }
        }
    }
}